<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:replace="common/base::pagetitle"></title>
    <link th:replace="common/base::static"/>
    <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/material/easyui.css}">
    <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/icon.css}">
</head>
<body>
<div class="layui-form" style="padding: 20px 30px 0 0;">

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 200px">类型</label>
        <div class="layui-input-inline">
            <select name="type"  lay-verify="required"  lay-filter="type">
                <option value="10">请选择</option>
                <option value="0">扣减</option>
                <option value="1">恢复</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 200px">金额（元）</label>
        <div class="layui-input-inline">
            <input type="text"  onkeyup="this.value=/^[0-9]*\.?[0-9]{0,2}$/.test(this.value) ? this.value : this.value.substring(0,this.value.length-1)" id="amount" name="amount" min="0" lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item" id="divFinancingId" style="display: none;">
        <label class="layui-form-label" style="width: 200px">关联凭证号</label>
        <div class="layui-input-inline">
            <input type="text"  id="financingId"  name="financingId"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" id="divAcctNo" style="display: none">
        <label class="layui-form-label" style="width: 200px">对方账号</label>
        <div class="layui-input-inline">
            <input type="text"  id="acctNo" name="acctNo"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 200px">摘要</label>
        <div class="layui-input-inline">
            <input type="text"  id="remark" name="remark"  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 200px">使用时间</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="useDate" id="useDate"
                   placeholder="请选择使用时间">
        </div>
    </div>


    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit="" lay-filter="kFormSubmit" id="kFormSubmit" value="确认添加">
        <input type="text" name="compId" th:value="${compId}" placeholder="保存ID" autocomplete="off" class="layui-input">
        <input type="text" name="bankId" th:value="${bankId}" placeholder="保存ID" autocomplete="off" class="layui-input">
    </div>
</div>

<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>
<script th:src="@{/static/plugins/easyui/easyloader.js}"></script>
<script type="text/javascript" th:inline="javascript">
    function checkNum (val) {
        document.getElementById('amount').value = val >= 0 ? val : 0
    }


    layui.use(['form','laydate'], function () {
        var $ = layui.$, form = layui.form,laydate = layui.laydate;


        //监听提交
        form.on('submit(kFormSubmit)', function (data) {
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            if(!field.useDate){
                wlbKit.warningMsg("使用时间不能为空");
                return;
            }

            var reg = /^[0-9]*\.?[0-9]{0,2}$/
            if(!reg.test(field.amount)) {
                wlbKit.warningMsg("金额格式不正确！");
                return;
            }
            $.ajax({
                type: req.type.post,
                url: field.id ? api.credit.creditUseAdd : api.credit.creditUseAdd,
                data: field,
                async: false,
                success: function (r) {
                    if (r.code === req.status.ok) {
                        parent.layui.table.reload('kContentTable'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    } else {
                        wlbKit.errorMsg(r.msg);
                    }
                }
            });

        });

        //监听类型选择
        form.on('select(type)', function (data) {
            // 扣减
            if(data.value == 0){
                document.getElementById("divFinancingId").style.display = "block";
                document.getElementById("divAcctNo").style.display = "none";
            }
            // 恢复
            else if(data.value == 1){
                document.getElementById("divAcctNo").style.display = "block";
                document.getElementById("divFinancingId").style.display = "none";
            }else if(data.value == 10){
                document.getElementById("divAcctNo").style.display = "none";
                document.getElementById("divFinancingId").style.display = "none";
            }
        });

        // 使用时间
        laydate.render({
            elem: '#useDate' //指定元素
        });


    })
    </script>
</body>
</html>
